﻿using static System.Convert;
using static System.Text.Encoding;

namespace CommLib.Security
{
    public static class Base64Encrypt
    {
        /// <summary>
        /// Base64加密
        /// </summary>
        /// <param name="encryptString">要加密的字符串</param>
        /// <exception cref="System.ArgumentNullException"></exception>
        /// <exception cref="System.Text.EncoderFallbackException"></exception>
        /// <returns></returns>
        public static string Encrypt(string encryptString) => string.IsNullOrEmpty(encryptString) ? null : ToBase64String(UTF8.GetBytes(encryptString));
        
        /// <summary>
        /// Base64解密
        /// </summary>
        /// <param name="decryptString">要解密的字符串</param>
        /// <exception cref="System.ArgumentException"></exception>
        /// <exception cref="System.ArgumentNullException"></exception>
        /// <exception cref="System.FormatException"></exception>
        /// <exception cref="System.Text.DecoderFallbackException"></exception>
        /// <returns></returns>
        public static string Decrypt(string decryptString) => string.IsNullOrEmpty(decryptString) ? null : UTF8.GetString(FromBase64String(decryptString));
    }
}
